package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.MenuDao;
import com.itheima.dao.UserDao;
import com.itheima.entity.PageResult;
import com.itheima.pojo.Menu;
import com.itheima.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.transaction.annotation.Transactional;
import redis.clients.jedis.JedisPool;

import java.util.List;
import java.util.Map;

/**
 * 菜单服务实现类
 *
 * @author ：zengjie
 * @date ：2020/11/16
 */
@Service(interfaceClass = MenuService.class)
@Transactional
public class MenuServiceImpl implements MenuService {

    @Autowired
    private MenuDao menuDao;

    @Autowired
    private UserDao userDao;

    @Autowired
    private JedisPool jedisPool;

    /**
     * 根据用户名查询所有权限对应的菜单
     *
     * @param username
     * @return
     */
    @Override
    public List<Menu> findAllMenu(String username) {
        return menuDao.findAllMenu(username);
    }

    /**
     * 获取二级菜单
     *
     * @param menuid
     * @param username
     * @return
     */
    @Override
    public List<Menu> findChildren(Integer menuid, String username) {
        return menuDao.findChildren(menuid, username);
    }

    /*
     * 菜单分页查询
     * */
    @Override
    public PageResult findPage(Integer currentPage, Integer pageSize, String queryString) {
        PageHelper.startPage(currentPage, pageSize);
        List<Menu> list = menuDao.findPage(queryString);
        PageInfo<Menu> pageInfo = new PageInfo<>(list);
        return new PageResult(pageInfo.getTotal(), pageInfo.getList());

    }

    @Override
    public void add(Menu menu) {

        Long count = menuDao.findMenuname(menu.getName());
        if (count > 0) {
            throw new RuntimeException("菜单名重复");
        }

        Integer parentId = menu.getParentMenuId();
        Menu parentMenu = menuDao.findById(parentId);//上级菜单
        String parentMenuPath1 = parentMenu.getPath();//上级菜单路径
        Integer priority = menu.getPriority();//新建菜单的优先级
        String path1 = "/" + parentMenuPath1 + "-" + priority;
        menu.setPath(path1);
        menu.setParentMenuId(parentId);
        //menu.setLevel(2);
        menuDao.add(menu);
        this.clearRedisOldData();

    }

    /*
     * 编辑菜单管理
     * */
    @Override
    public void update(Menu menu) {

        int count2 = menu.getLevel();
        if (count2 == 1) {
            throw new RuntimeException("此为父菜单不可更改");
        }
        menuDao.update(menu);
        this.clearRedisOldData();

    }

    /*
     * 角色查询
     * */
    @Override
    public List<Map> findAllRoles() {
        return menuDao.findAllRoles();
    }

    /*
     *查询父菜单
     * */
    @Override
    public Integer queryParenMenu(Integer id) {
        return menuDao.queryParenMenu(id);
    }

    /*
     *删除菜单
     *
     */
    @Override
    public void deleteById(Integer id) {
        menuDao.deleteById(id);
        this.clearRedisOldData();

    }

    /*
     * 根据菜单id查询数据
     * */
    @Override
    public Menu findById(Integer id) {
        return menuDao.findById(id);
    }


    /**
     * 删除所有的redis缓存
     */
    private void clearRedisOldData() {
        List<String> userNames = userDao.findAll();
        if (userNames != null) {
            for (String userName : userNames) {
                if (userName != null && userName.length() > 0) {
                    jedisPool.getResource().del(userName);
                }
            }
        }
    }


}
